﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Core.SimpleTemp.Api.FX
{
    /// <summary>
    /// 
    /// </summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]

    public class FXDropDownController : CoreApiController
    {
        private CoreDBContext _coreDBContext;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="coreDBContext"></param>
        public FXDropDownController(CoreDBContext coreDBContext)
        {
            _coreDBContext = coreDBContext;
        }

        #region 操作及相关作业活动

        /// <summary>
        /// 操作及相关作业活动 /作业活动
        /// </summary>
        /// <param name="str"></param>
        /// <param name="fX_OperationAndWorkDto"></param>
        /// <returns></returns>
        [HttpPost("OpeRelaActivActivity/{str?}")]

        public async Task<IActionResult> OpeRelaActivActivity(string str, FX_OperationAndWorkDto fX_OperationAndWorkDto)
        { 
            var baseQeury = _coreDBContext.FX_OperationAndWorks.Where(a => a.IsDeleted == false).Where(b=>b.CreateFactory== fX_OperationAndWorkDto.CreateFactory);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Jobactivity.Contains(str));
            var ret = await baseQeury.Select(a => new { Value = a.Jobactivity }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        /// <summary>
        /// 操作及相关作业活动 /作业任务::
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("OpeRelaActivTask/{str?}/{factoryId?}")]

        public async Task<IActionResult> OpeRelaActivTask(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.FX_OperationAndWorks.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Jobactivity.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.JobTask }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        #endregion

        #region 操作程序
        /// <summary>
        /// 操作程序 /作业项目
        /// </summary>
        /// <param name="str"></param>
        ///  <param name="fX_OperationDto"></param>
        /// <returns></returns>
        [HttpPost("OperatingProcedureActivity/{str?}")]

        public async Task<IActionResult> OperatingProcedureActivity(string str, FX_OperationDto fX_OperationDto )
        {
            var baseQeury = _coreDBContext.FX_Operation.Where(a => a.IsDeleted == false).Where(b=>b.CreateFactory== fX_OperationDto.CreateFactory);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.JobItem.Contains(str));
            var ret = await baseQeury.Select(a => new { Value = a.JobItem }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 操作程序 /作业步骤::
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("OperatingProcedureTask/{str?}/{factoryId?}")]

        public async Task<IActionResult> OperatingProcedureTask(string str, Guid? factoryId )
        {
            var baseQeury = _coreDBContext.FX_Operation.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.JobItem.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.JobSteps }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 操作程序 /操作行为::
        /// </summary>
        /// <param name="str"></param>
        ///  <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("OperatingProcedureBehaviour/{str?}/{factoryId?}")]

        public async Task<IActionResult> OperatingProcedureBehaviour(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.FX_Operation.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.JobSteps.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.OperateAction }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }
        #endregion

        #region 生产作业活动
        /// <summary>
        /// 生产作业活动 /作业活动
        /// </summary>
        /// <param name="str"></param>
        /// <param name="fX_WorkActivityDto"></param>
        /// <returns></returns>
        [HttpPost("OperationalActivitiesActivity/{str?}")]

        public async Task<IActionResult> OperationalActivitiesActivity(string str, FX_WorkActivityDto fX_WorkActivityDto )
        {
            var baseQeury = _coreDBContext.FX_WorkActivity.Where(a => a.IsDeleted == false).Where(b => b.CreateFactory == fX_WorkActivityDto.CreateFactory);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Jobactivity.Contains(str));
            var ret = await baseQeury.Select(a => new { Value = a.Jobactivity }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 生产作业活动 /作业任务:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("OperationalActivitiesTask/{str?}/{factoryId?}")]

        public async Task<IActionResult> OperationalActivitiesTask(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.FX_WorkActivity.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId );
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Jobactivity.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.JobTask }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        #endregion

        #region 检维修作业活动
        /// <summary>
        /// 检维修作业活动 /作业模块:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="fX_CheckRepairDto"></param>
        /// <returns></returns>
        [HttpPost("InspeMainOperaDeviceName/{str?}")]

        public async Task<IActionResult> InspeMainOperaDeviceName(string str , FX_CheckRepairDto fX_CheckRepairDto)
        {
            var baseQeury = _coreDBContext.FX_CheckRepair.Where(a => a.IsDeleted == false).Where(b=>b.CreateFactory== fX_CheckRepairDto.CreateFactory);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.JobModule.Contains(str));
            var ret = await baseQeury.Select(a => new { Value = a.JobModule }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 检维修作业活动 /维修项目:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("InspeMainOperaMainProjects/{str?}/{factoryId?}")]

        public async Task<IActionResult> InspeMainOperaMainProjects(string str, Guid? factoryId )
        {
            var baseQeury = _coreDBContext.FX_CheckRepair.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId );
            if (str != null)
                baseQeury = baseQeury.Where(a => a.JobModule.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.MaintanenceProject }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }



        /// <summary>
        /// 检维修作业活动 /作业活动:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("InspeMainOperaMainCate/{str?}/{factoryId?}")]

        public async Task<IActionResult> InspeMainOperaMainCate(string str, Guid? factoryId )
        {
            var baseQeury = _coreDBContext.FX_CheckRepair.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.MaintanenceProject.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.JobActivity }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }
        #endregion


        #region 检维修作业活动工具

        /// <summary>
        /// 检维修作业活动工具 /工具类别
        /// </summary>
        /// <param name="str"></param>
        ///  <param name="fX_CheckRepairToolDto"></param>
        /// <returns></returns>
        [HttpPost("InspectionJobActivitiesToolsClass/{str?}")]

        public async Task<IActionResult> InspectionJobActivitiesToolsClass(string str, FX_CheckRepairToolDto fX_CheckRepairToolDto )
        {
            var baseQeury = _coreDBContext.FX_CheckRepairTool.Where(a => a.IsDeleted == false).Where(b=>b.CreateFactory== fX_CheckRepairToolDto.CreateFactory);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.ToolType.Contains(str));
            var ret = await baseQeury.Select(a => new { Value = a.ToolType }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 检维修作业活动工具 /工具名称:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("InspectionJobActivitiesToolsName/{str?}/{factoryId?}")]

        public async Task<IActionResult> InspectionJobActivitiesToolsName(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.FX_CheckRepairTool.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.ToolType.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.ToolName }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        #endregion



        #region 设备设施
        /// <summary>
        /// 设备设施 /设备:
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpPost("EquipmentFacilitiesTypeDevice/{str?}")]

        public async Task<IActionResult> EquipmentFacilitiesTypeDevice(string str, FX_EquipmentDto fX_EquipmentDto)
        {
            var baseQeury = _coreDBContext.FX_Equipment.Where(a => a.IsDeleted == false).Where(b=>b.CreateFactory== fX_EquipmentDto.CreateFactory);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Device.Contains(str));
            var ret = await baseQeury.Select(a => new { Value = a.Device }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 设备设施 /功能部分:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("EquipmentFacilitiesFuncComp/{str?}/{factoryId?}")]

        public async Task<IActionResult> EquipmentFacilitiesFuncComp(string str, Guid? factoryId )
        {
            var baseQeury = _coreDBContext.FX_Equipment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Device.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.FunctionalPart }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        /// <summary>
        /// 设备设施 /子功能部分:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("EquipmentFacilitiesSubfuncComp/{str?}/{factoryId?}")]

        public async Task<IActionResult> EquipmentFacilitiesSubfuncComp(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.FX_Equipment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.FunctionalPart.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.Sub_functionPart }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        #endregion



        #region 工艺介质


        ///// <summary>
        ///// 工艺介质 /节点:
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //[HttpPost("ProcessMediumStep/{str?}")]

        //public async Task<IActionResult> ProcessMediumStep(string str)
        //{
        //    var baseQeury = _coreDBContext.FX_ProcessMedium.Where(a => a.IsDeleted == false);
        //    if (str != null)
        //        baseQeury = baseQeury.Where(a => a.Step.Contains(str));
        //    var ret = await baseQeury.Select(a => new { Value = a.Step }).Distinct().ToArrayAsync();
        //    return this.JsonSuccess(ret);
        //}



        /// <summary>
        /// 工艺介质 /要素/特性
        /// </summary>
        /// <param name="str"></param>
        /// <param name="fX_ProcessDto"></param>
        /// <returns></returns>
        [HttpPost("ProcessMediumElemeCharac/{str?}")]

        public async Task<IActionResult> ProcessMediumElemeCharac(string str , FX_ProcessDto fX_ProcessDto )
        {
            var baseQeury = _coreDBContext.FX_Process.Where(a => a.IsDeleted == false).Where(b=>b.CreateFactory== fX_ProcessDto.CreateFactory);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.FeatureCharacteristics.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.FeatureCharacteristics }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 工艺介质 /引导词:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("ProcessMediumInducWords/{str?}/{factoryId?}")]

        public async Task<IActionResult> ProcessMediumInducWords(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.FX_Process.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.FeatureCharacteristics.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.GuideWord }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        #endregion


        #region 场所环境



        /// <summary>
        /// 场所环境 /场所/环境:
        /// </summary>
        /// <param name="str"></param>
        ///  <param name="fX_ProcessDto"></param>
        /// <returns></returns>
        [HttpPost("EnvironmentEnvironment/{str?}")]

        public async Task<IActionResult> EnvironmentEnvironment(string str, FX_ProcessDto fX_ProcessDto)
        {
            var baseQeury = _coreDBContext.FX_Environment.Where(a => a.IsDeleted == false).Where(b => b.CreateFactory == fX_ProcessDto.CreateFactory); 
            if (str != null)
                baseQeury = baseQeury.Where(a => a.PlaceEnvironmentName.Contains(str));
            var ret = await baseQeury.Select(a => new { Value = a.PlaceEnvironmentName }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 场所环境 /能量源:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpPost("EnvironmentEnergySource/{str?}/{factoryId?}")]

        public async Task<IActionResult> EnvironmentEnergySource(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.FX_Environment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.PlaceEnvironmentName.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.EnergySource }).Distinct().ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        #endregion
    }
}